package com.activity;

import android.content.Intent;
import android.os.AsyncTask;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;

import android.widget.TextView;
import android.widget.Toast;


import com.tools.SomeMonitorEditText;

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Register2Activity extends AppCompatActivity {

    private TextView 标题;
    private ImageButton 后退按钮;
    private EditText 编辑用户名;
    private EditText 编辑密码;
    private EditText 再次输入密码;
    private CheckBox 男生;
    private CheckBox 女生;
    private Button 注册按钮;

    String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        标题 = (TextView) findViewById(R.id.标题);
        后退按钮 = (ImageButton) findViewById(R.id.后退按钮);
        编辑用户名 = (EditText) findViewById(R.id.编辑用户名);
        编辑密码 = (EditText) findViewById(R.id.编辑密码);
        再次输入密码 = (EditText) findViewById(R.id.再次输入密码);
        男生 = (CheckBox) findViewById(R.id.男生);
        女生 = (CheckBox) findViewById(R.id.女生);
        注册按钮 = (Button) findViewById(R.id.注册按钮);

        男生.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(男生.isChecked()){
                    女生.setChecked(false);
                }else{
                    女生.setChecked(true);
                }
            }
        });
        女生.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(女生.isChecked()){
                    男生.setChecked(false);
                }else{
                    男生.setChecked(true);
                }
            }
        });


        标题.setText("注册");

        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");

        //没写完无法提交
        new SomeMonitorEditText().SetMonitorEditText(注册按钮,编辑用户名,编辑密码,再次输入密码);


        后退按钮.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(Register2Activity.this, Register1Activity.class);
                startActivity(intent);
                Register2Activity.this.finish();
            }
        });

        注册按钮.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String uname = 编辑用户名.getText().toString().trim();
                String pwd = 编辑密码.getText().toString().trim();
                String rpwd = 再次输入密码.getText().toString().trim();
                int gender = 1;

                if(男生.isChecked()){
                    gender = 1;
                }else {
                    gender = 2;
                }

                if(!pwd.equals(rpwd)){
                    Toast.makeText(Register2Activity.this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
                    再次输入密码.setText(null);
                }else{
                    String path = "http://101.200.40.47:8080/stu/user/doRegister?userName="+uname+"&password="+pwd+"&gender="+gender+"&phone="+phone;
                    new doRegister().execute(path);
                }
            }
        });

    }

    //判断注册
    private class  doRegister extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {

            String path = params[0].toString();

            try {
                //以get方式提交整个路径
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");//这个应该都知道是干嘛的，默认是GET  
                conn.setConnectTimeout(5000);

                if (conn.getResponseCode() == 200) {
                    InputStream is = conn.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str = br.readLine();
                    return str;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            String info = o.toString().trim();
            if(info.equals("1")){
                Toast.makeText(Register2Activity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(Register2Activity.this, LoginActivity.class);
                startActivity(intent);
                Register2Activity.this.finish();
            }else if(info.equals("0")){
                Toast.makeText(Register2Activity.this, "用户名已被占用！", Toast.LENGTH_SHORT).show();
                编辑用户名.setText(null);
            }else{
                Toast.makeText(Register2Activity.this, "未知错误！注册失败！", Toast.LENGTH_SHORT).show();
            }
        }
    }

    long mExitTime;       //实现“再按一次退出”的记录时间变量
    @Override //再按一次退出程序
    public void onBackPressed() {
        if (System.currentTimeMillis() - mExitTime < 2000) {
            super.onBackPressed();
        } else {
            mExitTime = System.currentTimeMillis();
            Toast.makeText(Register2Activity.this,"再按一次退出应用",Toast.LENGTH_SHORT).show();
        }
    }
}
